package com.geostar.constructionland.index.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geostar.constructionland.index.entity.AjaxResult;
import com.geostar.constructionland.index.entity.InxStorageAcotEntity;
import com.geostar.constructionland.index.entity.OrgOrganization;
import com.geostar.constructionland.index.entity.model.IndexOrdinatingModel;
import com.geostar.constructionland.index.entity.model.IndexStatisticsModel;
import com.geostar.constructionland.index.mapper.OrgOrganizationMapper;
import com.geostar.constructionland.index.service.IndexService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yelll
 * @since 2020-1-11 10:07
 **/
@RestController
@RequestMapping("/index")
@Api(tags = "统计指标相关")
public class IndexController {
    @Resource
    IndexService indexService;

    @Value("${server.port}")
    private String port;
    @RequestMapping(value = "getIndex",method = RequestMethod.GET)
    public String getIndex(String message){
        return "收到信息内容{"+message+"},访问的服务端口："+port;
    }

    @ApiOperation(value = "查询指标统计信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "zbnf",value = "指标年份"),
            @ApiImplicitParam(name = "zblx",value = "指标类型"),
            @ApiImplicitParam(name = "xzqhdmProvince",value = "省级行政区划"),
            @ApiImplicitParam(name = "xzqhdmCity",value = "市级行政区划"),
            @ApiImplicitParam(name = "xzqhdmCountry",value = "县级行政区划"),
            @ApiImplicitParam(name = "userOrgId",value = "登陆用户的机构id"),
    })
    @PostMapping(value = "/indexStatisticsList")
    @ResponseBody
    public Map queryIndexStatisticsList(@Valid IndexStatisticsModel indexStatisticsModel){
        // 测试：userOrgId=934
            List result = indexService.queryIndexStatistics(indexStatisticsModel);
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("data",result);
            map.put("total",result.size());
            System.out.println(map.get("data"));
        return map;
    }

   @ApiOperation(value = "查询统筹台账信息")
   @ApiImplicitParams({
           @ApiImplicitParam(name = "pagenum",value = "页码",required = true),
           @ApiImplicitParam(name = "pagesize",value = "页数",required = true),
           @ApiImplicitParam(name = "zbnf",value = "指标年份"),
           @ApiImplicitParam(name = "arrZblx",value = "指标类型"),
           @ApiImplicitParam(name = "xzqhdmProvince",value = "省级行政区划"),
           @ApiImplicitParam(name = "xzqhdmCity",value = "市级行政区划"),
           @ApiImplicitParam(name = "xzqhdmCountry",value = "县级行政区划"),
           @ApiImplicitParam(name = "tclx",value = "统筹类型"),
           @ApiImplicitParam(name = "arrYwlx",value = "业务类型"),
           @ApiImplicitParam(name = "sortOrder",value = "排序顺序"),
           @ApiImplicitParam(name = "sortName",value = "排序字段")
   })
   @PostMapping(value = "/indexOrdinatingList")
   @ResponseBody
    public Map queryIndexOrdinating(IndexOrdinatingModel indexOrdinatingModel){
        Map<String,Object> map=new HashMap<String,Object>();

       Page<InxStorageAcotEntity> inxStorageAcotEntityPage = indexService.queryIndexOrdinating(indexOrdinatingModel);
       List resultList=new ArrayList();
       for(InxStorageAcotEntity storageAcotEntity :inxStorageAcotEntityPage.getRecords()){

            Map<String, Object> result=new HashMap<>();
                result.put("rkid",storageAcotEntity.getId());
                result.put("xzqhdm",storageAcotEntity.getDistCode());
                result.put("xzqhmc",storageAcotEntity.getDistName());
                result.put("zbnf",storageAcotEntity.getIndexYear());
                result.put("zbmc",storageAcotEntity.getIndexName());
                result.put("zblx",storageAcotEntity.getIndexType());
                result.put("ywlx",storageAcotEntity.getBusinessType());
                result.put("xzjsyd",storageAcotEntity.getBuildLandAdd());
                result.put("nyd",storageAcotEntity.getAgclLand());
                result.put("gd",storageAcotEntity.getFarmland());
                result.put("rksj", null==storageAcotEntity.getStorageTime() ? "":DateFormat.getDateInstance().format(storageAcotEntity.getStorageTime()));
                result.put("zbzt",storageAcotEntity.getIndexStatus());
                result.put("czrid",storageAcotEntity.getOperatorId());
                result.put("bz",storageAcotEntity.getRemark());
                result.put("shengxrq",null==storageAcotEntity.getEffectiveTime() ? "":DateFormat.getDateInstance().format(storageAcotEntity.getEffectiveTime()));
                result.put("shixrq",null==storageAcotEntity.getNonEffectiveTime() ? "":DateFormat.getDateInstance().format(storageAcotEntity.getNonEffectiveTime()) );
                result.put("xzqhdmProvince",storageAcotEntity.getDistCodeProvince());
                result.put("xzqhdmCity",storageAcotEntity.getDistCodeCity());
                result.put("xzqhdmCountry",storageAcotEntity.getDistCodeCountry());
                result.put("zxlx",storageAcotEntity.getSpecialItemType());
                result.put("tclx",storageAcotEntity.getOverallType());
                result.put("tczt",storageAcotEntity.getOverallStatus());
            resultList.add(result);
        }

       map.put("data",resultList);
       map.put("total",inxStorageAcotEntityPage.getTotal());

       return map;
    }

    @ApiOperation(value = "更新统筹状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "入库id",required = true),
            @ApiImplicitParam(name = "overallStatus",value = "统筹状态",required = true),
            @ApiImplicitParam(name = "remark",value = "备注")
    })
    @PostMapping(value = "/updateIndexOrdinating")
    public AjaxResult updateIndexOrdinating(@RequestParam(name = "id")  String id,@RequestParam(name = "overallStatus")  String overallStatus,
                                            @RequestParam(name = "remark")  String remark){

        indexService.updateIndexOrdinating(id,overallStatus,remark);
        return AjaxResult.success("");
    }


}
